{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "import itertools as it"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n下标0-3分别对应 Mike,Ellie, Rohan,Fatma\\n'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "下标0-3分别对应 Mike,Ellie, Rohan,Fatma\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('blue', 'red', 'green', 'purple'),\n",
       " ('blue', 'red', 'purple', 'green'),\n",
       " ('blue', 'green', 'red', 'purple'),\n",
       " ('blue', 'green', 'purple', 'red'),\n",
       " ('blue', 'purple', 'red', 'green'),\n",
       " ('blue', 'purple', 'green', 'red'),\n",
       " ('red', 'blue', 'green', 'purple'),\n",
       " ('red', 'blue', 'purple', 'green'),\n",
       " ('red', 'green', 'blue', 'purple'),\n",
       " ('red', 'green', 'purple', 'blue'),\n",
       " ('red', 'purple', 'blue', 'green'),\n",
       " ('red', 'purple', 'green', 'blue'),\n",
       " ('green', 'blue', 'red', 'purple'),\n",
       " ('green', 'blue', 'purple', 'red'),\n",
       " ('green', 'red', 'blue', 'purple'),\n",
       " ('green', 'red', 'purple', 'blue'),\n",
       " ('green', 'purple', 'blue', 'red'),\n",
       " ('green', 'purple', 'red', 'blue'),\n",
       " ('purple', 'blue', 'red', 'green'),\n",
       " ('purple', 'blue', 'green', 'red'),\n",
       " ('purple', 'red', 'blue', 'green'),\n",
       " ('purple', 'red', 'green', 'blue'),\n",
       " ('purple', 'green', 'blue', 'red'),\n",
       " ('purple', 'green', 'red', 'blue')]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "_color_list = list(it.permutations(['blue', 'red', 'green', 'purple'], 4))\n",
    "_rank_list = list(it.permutations(['1','2', '3', '4'], 4))\n",
    "_color_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['purple', 'blue', 'red', 'green'],\n",
       " ['purple', 'red', 'blue', 'green'],\n",
       " ['purple', 'green', 'blue', 'red']]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "color_list=[]\n",
    "for i in _color_list:\n",
    "    # Ellie骑紫色的车，Rohan没有骑绿色的车。\n",
    "    # 骑蓝色车的人领先Fatma。\n",
    "    if i[0] == 'purple' and i[2] != 'green' and i[3] != 'blue':\n",
    "        color_list.append(list(i))\n",
    "color_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['2', '3', '1', '4'],\n",
       " ['2', '3', '4', '1'],\n",
       " ['3', '2', '1', '4'],\n",
       " ['3', '2', '4', '1']]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rank_list=[]\n",
    "for i in _rank_list:\n",
    "    # Mike和Ellie不是第一，也不是最后。\n",
    "    if i[0] != '1' and i[0] != '4':\n",
    "        if i[1] != '1' and i[1] != '4':\n",
    "            rank_list.append(list(i))\n",
    "rank_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mike,Ellie, Rohan,Fatma\n",
      "名次['2', '3', '4', '1']\n",
      "颜色['purple', 'green', 'blue', 'red']\n"
     ]
    }
   ],
   "source": [
    "# 骑蓝色车的人领先Fatma\n",
    "# 骑绿色车的人领先骑紫色车的人\n",
    "for color in color_list:\n",
    "    blue_person = color.index('blue')\n",
    "    green_person = color.index('green')\n",
    "    purple_person = color.index('purple')\n",
    "    for r in rank_list:\n",
    "        if int(r[blue_person]) > int(r[3]):\n",
    "            if int(r[green_person]) > int(r[purple_person]):\n",
    "                print(\"Mike,Ellie, Rohan,Fatma\\n名次{}\\n颜色{}\".format(r, color))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
